import Vue from "vue";
import Router from "vue-router";
import Login from "./components/Login.vue";
import Home from "./components/Home.vue";

// home里面的子组件
import page from "./views/page.vue";
import commodity from "./views/commodity/commodity.vue";
import addgoogds from "./views/commodity/addgoogds.vue";
import addgory from "./views/commodity/addgory.vue";
import songory from "./views/commodity/songory.vue";

import category from "./views/commodity/category.vue";
import orderform from "./views/orderform/orderform.vue";
import lineitem from "./views/orderform/lineitem.vue";
import consumer from "./views/consumer.vue";

Vue.use(Router);

var router = new Router({
    routes: [
        {path: "/", redirect: "/login"},
        {path: "/login", component: Login},
        {
            path: "/home",
            component: Home,
            redirect: "/home/page",
            children: [
                {path: "/home/page", component: page},
                {path: "/home/commodity", component: commodity},
                {path: "/home/category", component: category},
                {path: "/home/orderform", component: orderform},
                {path: "/home/consumer", component: consumer},
                {path: "/home/lineitem", name: "lineitem", component: lineitem},
                {path: "/home/addgoogds", name: "addgoogds", component: addgoogds},
                {path: "/home/addgory", component: addgory},
                {path: "/home/songory", name: "songory", component: songory},
            ],
        },
    ],
});

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // form 代表从那个路径跳转而来
    // next 是一个函数，表示放行
    // next() 放行  next(./login) 强制跳转登录页
    if (to.path === "/login") return next();
    // 获取token
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) return next("/login");
    next();
});

export default router;
